﻿using Report.WebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Report.WebApi.Common
{
    public static class HelpTool
    {
        public static YearMonthModel GetMonthsThisYear(QueryRequestModel request)
        {
            var yearModel = new YearMonthModel();
            yearModel.Months = new List<int>();
            yearModel.Days = new List<int>();
            yearModel.WeekDays = new List<int>();

            if (request.QueryByYear)
            {
                var year = DateTime.Now.Year;
                yearModel.Year = year;
                var month = DateTime.Now.Month;
                for (int i = 1; i <= month; i++)
                {
                    yearModel.Months.Add(i);
                }
                yearModel.QueryBegin = int.Parse(DateTime.Now.AddMonths(0 - yearModel.Months.Count + 1).AddYears(-1).ToString("yyyyMM"));
                yearModel.QueryEnd = int.Parse(DateTime.Now.AddYears(-1).ToString("yyyyMM"));

            }

            if (request.QueryByMonth)
            {

            }

            if (request.QueryByWeek)
            {

            }

            return yearModel;
        }
        public static YearMonthModel GetPrePeriod()
        {
            var yearModel = new YearMonthModel();
            yearModel.Months = new List<int>();
            var year = DateTime.Now.Year - 1;
            yearModel.Year = year;
            var month = DateTime.Now.Month;
            for (int i = 1; i <= month; i++)
            {
                yearModel.Months.Add(i);
            }
            return yearModel;
        }

        public static string ParamBuilder(QueryRequestModel request)
        {
            var partitionKey = DateTime.Now.ToString("yyyyMMdd");
            var filterSB = new StringBuilder();
            filterSB.Append($"(PartitionKey eq '{partitionKey}')");

            if (!string.IsNullOrEmpty(request.QueryYear))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(AddDateYear eq '{request.QueryArea}')");

            }
            if (!string.IsNullOrEmpty(request.QueryMonth))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(AddDateMonth eq '{request.QueryMonth}')");

            }
            if (!string.IsNullOrEmpty(request.QueryStore))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(StoreName eq '{request.QueryStore}')");

            }
            if (!string.IsNullOrEmpty(request.QueryMntanCategory))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(MntCategory eq '{request.QueryMntanCategory}')");

            }
            if (!string.IsNullOrEmpty(request.QueryPriority))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(PriorityName_EN eq '{request.QueryPriority}')");

            }
            if (!string.IsNullOrEmpty(request.QueryStatus))
            {
                filterSB.Append(" and ");
                filterSB.Append($"(MntStatus eq '{request.QueryStatus}')");
            }
            return filterSB.ToString();
        }
    }
}
